package com.nbpt;

import android.content.Intent;
import android.net.UrlQuerySanitizer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class LoginActivity extends AppCompatActivity {


    HttpURLConnection connection = null;
    private Button btn;
    private String login_denglu="{\n" +
            "  \"Account\": \"15867576437\",\n" +
            "  \"Password\": \"a473033593\",\n" +
            "  \"IsRememberMe\": true\n" +
            "}";
    private String server_dizhi="http://api.nlecloud.com/Users/Login";
    private Thread thread;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        thread = new Thread(new network());
    }

    public class network implements Runnable
    {

        @Override
        public void run() {

            BufferedReader reader = null;
            try
            {
                URL url = new URL(server_dizhi);
                connection =(HttpURLConnection)url.openConnection();
                connection.setRequestMethod("POST");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                connection.setRequestProperty("connection","Keep-Alive");
                connection.setRequestProperty("Charset","UTF-8");
                connection.setRequestProperty("Content-Type","application/json");

                OutputStream outputStream=connection.getOutputStream();
                outputStream.write(login_denglu.getBytes());
                outputStream.flush();
                outputStream.close();


                if (connection.getResponseCode()==200)
                {
                    Message msg = Message.obtain();
                    msg.what = MSG_cg;
                    InputStream inputStream = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder response= new StringBuilder();
                    String line;
                    while((line=reader.readLine())!=null)
                    {
                        response.append(line);
                    }
                    Log.e("TAG","云服务器返回:"+response.toString());
                    handler.sendMessage(msg);
                    Intent intent = new Intent(LoginActivity.this,SmartCity.class);
                    startActivity(intent);

                }
                else
                {
                    Message msg = Message.obtain();
                    msg.what = MSG_sb;
                    handler.sendMessage(msg);
                    Log.e("TAG","登陆失败，返回结果："+connection.getResponseCode());
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally {

            }






        }

    }

    private final int MSG_cg = 0;
    private final int MSG_sb = 1;
    public Handler handler= new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what)
            {
                case MSG_cg:
                    Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
                    break;
                case MSG_sb:
                    Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };

    public  void  cloudLogin(View view)
    {
        thread.start();

    }
}
